/**
 * Created by yunpeng on 2014/12/5.
 */
//var db = require('../utils/dbConn');
var Table = require('../utils/commonDBConn').Table;

var LifeService = function(stypeid,lname,lprice,describe,tel,address,coords,pic,cid,createtime,recommend){
    this.lid=null;
    this.stypeid = stypeid;
    this.lname = lname;
    this.lprice = lprice;
    this.describe = describe;
    this.tel = tel;
    this.address = address;
    this.coords = coords;
    this.pic = pic;
    this.cid = cid;
    this.createtime = createtime;
    this.recommend = recommend;

    if(typeof this.save !='function'){
        LifeService.prototype.save = function(users,callback){
            var listService = this;
        new Table().getConnection(function(connection){
            connection.beginTransaction(function(err) {
                if (err) { //throw err;
                    callback(false);
                }
                connection.query('INSERT INTO life_service SET ?', listService, function(err, result) {
                    if (err) {
                        connection.rollback(function() {
                            //throw err;
                            callback(false);
                        });
                    }

                    var log = 'Post ' + result.insertId + ' added';
                    users.lid = result.insertId;//商家的ID
                    users.usertype = 2;//用户类型为商家用户
                        connection.query('INSERT INTO users SET ?', users, function(err, result) {
                        if (err) {
                            connection.rollback(function() {
                                //throw err;
                                callback(false);
                            });
                        }
                        connection.commit(function(err) {
                            if (err) {
                                connection.rollback(function() {
                                    //throw err;
                                    callback(false);
                                });
                            }else{
                                connection.release(); //release
                                callback(true);
                            }
                        });
                    });
                });
            });
        });
        }
        /*LifeService.prototype.save = function(callback){
            new Table('life_service').insert(this,function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
            //console.log('ssssaaa');
        }*/
    }

    if(typeof this.update !='function'){
        LifeService.prototype.update = function(callback){
            new Table('life_service').update('lid', this, function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
            //console.log('ssssaaa');
        }
    }

    if(typeof this.delBus !='function'){
        LifeService.prototype.delBus = function(ids, callback){
            new Table('life_service').delete('lid', ids, function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
            //console.log('ssssaaa');
        }
    }

    if(typeof this.getList !='function'){//商家列表
        LifeService.prototype.getList = function(params,page,callback){
            var columns = ' l.lid,l.stypeid,l.lname,l.lprice,l.describe,l.tel,l.address,l.coords,l.pic,l.cid,date_format(l.createtime,"%Y-%m-%d %H:%i:%s") as createtime,l.recommend,c.province,c.city,c.firstletter,s.serv_name,s.describe AS sd ';
            var condition1 =' AND l.cid=c.cid AND s.serv_id=l.stypeid ';
            if(params[0]&&params[0]!='null'){//开始时间
                condition1+=" AND l.createtime>'"+params[0]+"'";
            }
            if(params[1]&&params[1]!='null'){//结束时间
                condition1+=" AND l.createtime<'"+params[1]+"'";
            }
            if(params[2]&&params[2]!='null'){//商家名称模糊查询
                condition1+=" AND l.lname LIKE '%"+params[2]+"%'";
            }
            if(params[3]&&params[3]!='null'){
                condition1+=" AND s.serv_type = '"+params[3]+"'";
            }
            if(params[4]&&params[4]!='null'){//商家名称模糊查询
                condition1+=" AND l.cid = '"+params[4]+"'";
            }
            if(params[5]&&params[5]!='null'){//商家名称模糊查询
                condition1+=" AND l.lid = '"+params[5]+"'";
            }
            if(params[6]&&params[6]!='null'){//商家名称模糊查询
                condition1+=" AND s.serv_id = '"+params[6]+"'";
            }
            if(params[7]&&params[7]!='null'){//商家名称模糊查询
                condition1+=" AND l.recommend = '"+params[7]+"'";
            }
            new Table('life_service l,citys c,service_type s').queryPage(columns,condition1,page,function(err,result){
                //console.log(result);
                callback(result);
            });
            /*db.getPool.getConnection(function(error, connection) {
                var sql = 'SELECT l.lid,l.stypeid,l.lname,l.lprice,l.describe,l.address,l.coords,l.pic,l.createtime,' +
                    'c.province,c.city,c.firstletter,s.serv_name,s.describe AS sd from life_service l,citys c,service_type s' +
                    ' WHERE l.cid=c.cid AND s.serv_id=l.stypeid ORDER BY ? ? LIMIT ? , ?';
                connection.query(sql,page,function(err,result){
                    callback(result);
                });
            });*/

            /*db.execQuery(sql,page,function(result){
                console.log(result);
                callback(result);
            });*/

        }
    }


    if(typeof this.getListOfUser !='function'){//商家列表
        LifeService.prototype.getListOfUser = function(params,page,userid,callback){
            var columns = ' l.lid,l.stypeid,l.lname,l.lprice,l.describe,l.tel,l.address,l.coords,l.pic,date_format(l.createtime,"%Y-%m-%d %H:%i:%s") as createtime,c.province ';
            var condition1 =' AND   l.lid in (SELECT lg.lid FROM user_lifegoods ul,life_goods lg WHERE ul.gid=lg.gid AND ul.userid='+userid+' GROUP BY lg.lid) AND l.cid=c.cid   ';

            if(params[0]){//商家名称模糊查询
                condition1+=" AND l.lid = '"+params[5]+"'";
            }
            new Table('life_service l,citys c').queryPage(columns,condition1,page,function(err,result){
                //console.log(result);
                callback(result);
            });


        }
    }
}

module.exports.LifeService = LifeService;